package com.wt.admin.util;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.List;
import java.util.function.Function;

public class PageUtil{

    @Data
    public static class PageDTO<T> {
        private T data;

        private Long current = 1L;

        private Long size = 20L;
    }

    @Data
    public static class PageVO<T> {

        private Long current;

        private Long size;

        private Long total;

        private List<T> records;

        public PageVO(PageDTO pageDTO) {
            this.current = pageDTO.getCurrent();
            this.size = pageDTO.getSize();
        }

        public PageVO() {

        }

        public Long getCurrent() {
            return current;
        }

        public PageVO<T> setCurrent(Long current) {
            this.current = current;
            return this;
        }

        public Long getSize() {
            return size;
        }

        public PageVO<T> setSize(Long size) {
            this.size = size;
            return this;
        }

        public Long getTotal() {
            return total;
        }

        public PageVO<T> setTotal(Long total) {
            this.total = total;
            return this;
        }

        public List<T> getRecords() {
            return records;
        }

        public PageVO<T> setRecords(List<T> records) {
            this.records = records;
            return this;
        }
    }


    public static <T, R> PageVO<R> findPage(PageDTO<T> pageDTO, Function<Page<T>, IPage<R>> fun) {
        Page<T> page = new Page<T>(pageDTO.getCurrent(), pageDTO.getSize());
        IPage<R> iPage = fun.apply(page);
        return new PageVO<R>()
                .setCurrent(pageDTO.getCurrent())
                .setSize(pageDTO.getSize())
                .setTotal(iPage.getTotal())
                .setRecords(iPage.getRecords());
    }


}
